<%@Page Language="VB"%>

<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.OleDb" %>

<%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>The .NET OleDbDataReader and OleDbConnection Objects</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">The .NET OleDbDataReader and OleDbConnection Objects</span><hr />
<!--------------------------------------------------------------------------->

<%'-- insert connection string script --%>
<wrox:connect id="ctlConnectStrings" runat="server"/>

<div>Connection string: <b><span id="outConnect" runat="server"></span></b></div>
<div>SELECT command: <b><span id="outSelect" runat="server"></span></b></div>
<div id="outError" runat="server">&nbsp;</div>
<div id="outResult" runat="server"></div>

<script language="vb" runat="server">

Sub Page_Load()

   'declare a string to hold the results as an HTML table
   Dim strResult As String = "<table>"

   'get connection string from ..\global\connect-strings.ascx user control
   Dim strConnect As String
   strConnect = ctlConnectStrings.OLEDBConnectionString
   outConnect.InnerText = strConnect 'and display it

   'specify the SELECT statement to extract the data
   Dim strSelect As String
   strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '1861003%'"
   outSelect.InnerText = strSelect   'and display it

   Try

      'create a new Connection object using the connection string
      Dim objConnect As New OleDbConnection(strConnect)

      'open the connection to the database
      objConnect.Open()

      'create a new Command using the connection object and select statement
      Dim objCommand As New OleDbCommand(strSelect, objConnect)

      'declare a variable to hold a DataReader object
      Dim objDataReader As OleDbDataReader

      'execute the SQL statement against the command to fill the DataReader
      objDataReader = objCommand.ExecuteReader()

      'iterate through the records in the DataReader getting field values
      'the Read method returns False when there are no more records
      Do While objDataReader.Read()

         strResult += "<tr><td>" & objDataReader("ISBN") & "</td><td> &nbsp;" _
                   & objDataReader("Title") & "</td><td> &nbsp;" _
                   & objDataReader("PublicationDate") & "</td></tr>"

      Loop

      'close the DataReader and Connection
      objDataReader.Close()
      objConnect.Close()

   Catch objError As Exception

      'display error details
      outError.InnerHtml = "<b>* Error while accessing data</b>.<br />" _
          & objError.Message & "<br />" & objError.Source
      Exit Sub  ' and stop execution

   End Try

   'add closing table tag and display the results
   strResult += "</table>"
   outResult.InnerHtml = strResult

End Sub
</script>


<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
